<?php
/**
 * 优惠券
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisCoupon;
use app\ais\model\AisStore;
use app\ais\model\AisCouponUser;
use app\ais\model\AisConfig;
use app\ais\model\AisStoreUnion;
use think\Db;

class Couponuser extends Base{

    protected $config;

    /**
     * 初始化当前应用管理员是不是联盟城市账户
     * @return void
     */
    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
        $this->config = AisConfig::getConfig($this->member_miniapp_id);
    }

    /**
    * 用户已领优惠券
    * @return void
    */
    public function index(){
        $param['type'] = $this->request->param('type/d',0);
        $param['page'] = $this->request->param('page/d',1);
        $this->apiSign($param);
        $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
        $condition[] = ['uid','=',$this->user->id];
        $condition[] = ['money','=',0];
        $condition[] = ['is_end','=',0];
        switch ($param['type']){
            case 1:
                $condition[] = ['types','<=',1];
                break;
            case 2:
                $condition[] = ['types','=',2];
                break;
            case 3:
                $condition[] = ['types','=',3];
                break;
        }
        $coupon = AisCouponUser::with(['store' => function($query) {
            $query->field('id,name,img');
        }])->where($condition)->order('create_time desc')->page($param['page'],10)->select();
        if ($coupon->isEmpty()) {
            return enjson(204);
        }
        foreach ($coupon as $key => $info){
            $coupon[$key]->end_time = $info->create_time + $this->config->coupon_longtime * 60 * 60;
        }
        return enjson(200,$coupon);
    }

    /**
     * 领取优惠券
     * @return void
     */
    public function getCoupon(){
        $param['id']        = $this->request->param('id');
        $param['store_id']  = $this->request->param('store_id'); //引荐的店铺id
        $this->apiSign($param);
        $couponUser = AisCouponUser::where(['member_miniapp_id' => $this->miniapp_id,'coupon_id' => $param['id'],'uid' => $this->user->id,'is_end' => 0])->find();
        if(!empty($couponUser)){
            return enjson(200,'已经拥有该优惠券',$couponUser);
        }
        if($param['id']){
            $info = AisCoupon::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'is_end' => 0])->find();
            if(!empty($info)){
                $is_group = AisStoreUnion::isGroup($param['id'],$param['store_id'],$this->miniapp_id);
                $data['member_miniapp_id'] = $this->miniapp_id;
                $data['store_id']          = $info->store_id;
                $data['coupon_id']         = $info->id;
                $data['uid']               = $this->user->id;
                $data['is_end']            = 0;
                $data['types']             = $info->types;
                $data['name']              = $info->name;
                $data['price']             = $info->price;
                $data['discount']          = $info->discount;
                $data['howmuch']           = $info->howmuch;
                $data['amount']            = $info->amount;
                $data['tips']              = $info->tips;
                $data['group_ids']         = $is_group ?? 0;
                $data['parent_store_id']   = !empty($param['store_id']) ? $param['store_id'] : 0;
                $data['update_time']       = time();
                $data['create_time']       = time();
                $rel = AisCouponUser::create($data);
                if($rel){
                    return enjson(200,$rel);
                }
                return enjson(204);
            }
        }
        return enjson(403,'未找到优惠券');
    }

    /**
     * 用户优惠券查看
     * @return void
     */
    public function read(){
        $param['id'] = $this->request->param('id'); 
        $this->apiSign($param);
        $info = AisCouponUser::with(['coupon' => function($query) {
            $query->field('id,create_time');
        },'store' => function($query) {
            $query->field('id,name,address,longitude,latitude,img,telphone');
        }])->where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
        if (empty($info)) {
            return enjson(404,'未找到优惠券');
        }
        $info->store->img =  $info->store->img;
        if(!empty($this->lbs)){
            $info->store->distance = getDistance($this->lbs->lng,$this->lbs->lat,$info->store->longitude,$info->store->latitude);
        }
        return enjson(200,$info);
    }

}